﻿//Created by Action Script Viewer - http://www.buraks.com/asv
package Transactions {
    import Engine.Managers.*;
    import Classes.*;
    import Modules.realtime.*;

    public class TPlaceMapResource extends TWorldState {

        private var m_params:Object;
        private var m_resource:MapResource;
        protected var m_originalTrees:Array;
        protected var m_trees:Array;

        public function TPlaceMapResource(_arg1:MapResource, _arg2:Boolean=false, _arg3:int=0){
            var _local6:Array;
            var _local7:Object;
            this.m_originalTrees = [];
            this.m_trees = [];
            super(_arg1);
            this.m_resource = _arg1;
            this.m_params = new Object();
            this.m_params.isGift = _arg2;
            this.m_params.source = _arg3;
            this.m_params.energyCost = 0;
            this.m_params.itemOwner = Global.player.uid;
            this.m_params.mapOwner = Global.world.ownerId;
            this.m_originalTrees = Global.world.wildernessSim.processExpansionsOnObjectPlace(this.m_resource);
            var _local4:int;
            var _local5:int;
            for each (_local6 in this.m_originalTrees) {
                this.m_trees.push(new Array());
                for each (_local7 in _local6) {
                    this.m_trees[_local5][_local4] = {
                        x:_local7.x,
                        y:_local7.y,
                        dir:_local7.dir,
                        itemName:_local7.itemName,
                        id:_local7.id
                    };
                    _local4++;
                };
                _local5++;
            };
            this.m_params.wildernessData = this.m_trees;
        }
        override public function perform():void{
            signedWorldAction("place", this.m_params);
        }
        override protected function onWorldActionComplete(_arg1:Object):void{
            var _local2:LotSite;
            var _local3:Array;
            var _local4:int;
            var _local5:Array;
            var _local6:int;
            var _local7:int;
            var _local8:GameObject;
            var _local9:int;
            if ((this.m_resource is LotSite)){
                _local2 = (this.m_resource as LotSite);
                Global.world.citySim.lotManager.addLotSite(_local2);
            };
            if (_arg1){
                if (_arg1["wildernessObjects"]){
                    _local3 = (_arg1["wildernessObjects"] as Array);
                    if (_local3){
                        _local4 = 0;
                        while (_local4 < _local3.length) {
                            _local5 = _local3[_local4];
                            if (_local5){
                                _local6 = 0;
                                if (this.m_originalTrees[_local4].length == _local5.length){
                                    _local7 = 0;
                                    while (_local7 < this.m_originalTrees[_local4].length) {
                                        _local8 = GameObject(this.m_originalTrees[_local4][_local7].wildernessObj);
                                        if (_local8.getId() == _local5[_local7].id){
                                            _local9 = _local5[_local7].newId;
                                            if (_local9 != 0){
                                                _local8.setId(_local9);
                                                _local6++;
                                            };
                                        };
                                        _local7++;
                                    };
                                };
                                if (_local6 != this.m_originalTrees[_local4].length){
                                    ErrorManager.addError(((("Warning: TPlaceMapResource failed to remap ids. Expected " + this.m_originalTrees[_local4].length) + " got ") + _local6));
                                };
                            };
                            _local4++;
                        };
                    };
                };
            };
        }
        override public function getRealtimeMethod():RealtimeMethod{
            return (new RealtimeMethod("showAction", "placed", this.m_resource.getItem().name));
        }

    }
}//package Transactions 
